import query from './atom/db/query';
import count from './atom/db/count';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	criteria: object,
	page_size: number,
	page_no: number,
}

function format(d: number) {
	const dt = new Date(d);
	return dt.getFullYear() + '-' + (((dt.getMonth() + 1) < 10) ? ('0' + (dt.getMonth() + 1)) : (dt.getMonth() + 1)) + '-' + ((dt.getDate() < 10) ? ('0' + (dt.getDate())) : (dt.getDate()));
}
async function atom(msg: Message) {
	const systemid = '5981';
	const q = query(action_id, session_id, spaceid, systemid);
	const c = count(action_id, session_id, spaceid, systemid);
	q.prepare('exam', ['_id', 'exam_id', 'exam_name', 'fzno', 'start_date', 'finish_date', 'create_time', 'creater', 'campus_id', 'campus_name', 'days', 'default_val', 'am_start_time', 'pm_start_time'], msg.criteria, msg.page_size, msg.page_no, [], ['start_date']);
	const [t] = await q.exec();
	t.forEach((item) => {
		item['start_date_str'] = format(item['start_date']);
		item['finish_date_str'] = format(item['finish_date']);
		item['create_time_str'] = format(item['create_time']);
	});
	c.prepare('exam', msg.criteria);
	const [cnt] = await c.exec();
	return { count: cnt, dataArray: t };
}